import { defineStore } from "pinia";
import { ref, computed } from "vue";

export const useCityStore = defineStore("city", () => {
  // 用ref来创建仓库数据
  const cityName = ref<string>(localStorage.getItem("name") || "郑州");

  // 用computed来创建计算属性(getters)
  const reverseCity = computed(() => {
    return cityName.value.split("").reverse().join("");
  });

  // 用函数来创建修改数据的方法(actions)
  // 同步异步都能写
  const changeCity = (name: string) => {
    setTimeout(() => {
      cityName.value = name;
    }, 2000);
  };

  return { cityName, reverseCity, changeCity };
});
